package com.yiwei.ydd.constant;

import android.text.TextUtils;

import com.yiwei.ydd.Application;
import com.yiwei.ydd.api.model.InitAppModel;
import com.yiwei.ydd.api.model.LoginModel;
import com.yiwei.ydd.api.model.UserInfoModel;
import com.yiwei.ydd.event.LoginSuccessEvent;
import com.yiwei.ydd.event.LogoutEvent;
import com.yiwei.ydd.util.DateTime;
import com.yiwei.ydd.util.PreferenceUtils;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by xsooy-pc on 18-3-7.
 */

public class UserInfoGlobal {
    public static void saveInitData(InitAppModel.DataBean item) {
        PreferenceUtils.putString(Application.getInstance(), "is_login", item.is_logined);
        PreferenceUtils.putString(Application.getInstance(), "lastest_version", item.version.getLastest_version());
        PreferenceUtils.putInt(Application.getInstance(), "forced_upgrade", item.version.getForced_upgrade());

        PreferenceUtils.putString(Application.getInstance(), "download_url", item.version.getDownload_url());
        PreferenceUtils.putString(Application.getInstance(), "upgrade_msg", item.version.getUpgrade_msg());
        PreferenceUtils.putString(Application.getInstance(), "level_up", item.level_up);
        PreferenceUtils.putString(Application.getInstance(), "is_trueshop", item.is_trueshop);
        PreferenceUtils.putString(Application.getInstance(), "support", item.services.support);
        PreferenceUtils.putString(Application.getInstance(), "combo", item.services.combo);
        PreferenceUtils.putString(Application.getInstance(), "questions", item.services.questions);
        PreferenceUtils.putString(Application.getInstance(), "oilcard", item.services.oilcard);
    }


    public static void saveLoginInfo(LoginModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "email", item.email);
        PreferenceUtils.putString(Application.getInstance(), "icon", item.icon);
        PreferenceUtils.putString(Application.getInstance(), "phone", item.phone);
        PreferenceUtils.putString(Application.getInstance(), "username", item.username);


        login();
    }

    public static void saveUserInfo(UserInfoModel.DatasBean item) {
        PreferenceUtils.putString(Application.getInstance(), "icon", item.icon);
        PreferenceUtils.putString(Application.getInstance(), "phone", item.phone);
        PreferenceUtils.putString(Application.getInstance(), "username", item.username);
        PreferenceUtils.putString(Application.getInstance(), "birthday", item.birthday);
        PreferenceUtils.putString(Application.getInstance(), "sex", item.sex);
        PreferenceUtils.putInt(Application.getInstance(), "level", item.level);
        PreferenceUtils.putString(Application.getInstance(), "money", item.money);
        PreferenceUtils.putString(Application.getInstance(), "invite_code", item.invite_code);
        PreferenceUtils.putString(Application.getInstance(), "invite_qrcode_url", item.invite_qrcode_url);
        PreferenceUtils.putString(Application.getInstance(), "commission", item.commission);
        PreferenceUtils.putString(Application.getInstance(), "uolevel_id", item.uolevel_id);
        PreferenceUtils.putString(Application.getInstance(), "utlevel_id", item.utlevel_id);
        PreferenceUtils.putString(Application.getInstance(), "isset_pay_password", item.isset_pay_password);
        PreferenceUtils.putString(Application.getInstance(), "consume_money", item.consume_money);
    }

    public static boolean getIsSetPayPass() {
        if (PreferenceUtils.getString(Application.getInstance(), "isset_pay_password", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean getIsLevelUp() {
        if (PreferenceUtils.getString(Application.getInstance(), "level_up", "0").equals("1")) {
            return true;
        } else {
            return false;
        }

    }

    public static void setPayPass() {
        PreferenceUtils.putString(Application.getInstance(), "isset_pay_password", "1");
    }

    public static String getConsumeMoney() {
        return PreferenceUtils.getString(Application.getInstance(), "consume_money", "0");
    }

    public static String getCombo() {
        return PreferenceUtils.getString(Application.getInstance(), "combo", "");
    }

    public static String getQuestions() {
        return PreferenceUtils.getString(Application.getInstance(), "questions", "");
    }

    public static String getBalance() {
        return PreferenceUtils.getString(Application.getInstance(), "money", "0");
    }

    public static String getCommentInfo() {
        return PreferenceUtils.getString(Application.getInstance(), "uolevel_id", "");
    }

    public static String getInviteCode() {
        return PreferenceUtils.getString(Application.getInstance(), "invite_code", "");
    }


    public static String getUserSex() {
        if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("1")) {
            return "男";
        } else if (PreferenceUtils.getString(Application.getInstance(), "sex", "").equals("2")) {
            return "女";
        } else return "未设置";
    }

    public static String getHeader() {
        return PreferenceUtils.getString(Application.getInstance(), "header", "");
    }

    public static String getMobile() {
        return PreferenceUtils.getString(Application.getInstance(), "mobile", "");
    }

    public static String getUserBirth() {
        if (TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "birthday", ""))) {
            return "未设置";
        } else {
            return DateTime.toDate(Long.valueOf(PreferenceUtils.getString(Application.getInstance(), "birthday", "")) * 1000);
        }
    }

    public static String getUserPhone() {
        return PreferenceUtils.getString(Application.getInstance(), "phone", "");
    }

    public static String getNowCity() {
        return PreferenceUtils.getString(Application.getInstance(), "now_city", "");
    }

    public static void setNowCity(String city) {
        PreferenceUtils.putString(Application.getInstance(), "now_city", city);
    }

    public static int getUserLevel() {
        return PreferenceUtils.getInt(Application.getInstance(), "level", 1);
    }

    public static boolean getNowStepLead() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "now_step_lead", true);
    }

    public static void setNowStepLead(boolean now_step_lead) {
        PreferenceUtils.putBoolean(Application.getInstance(), "now_step_lead", now_step_lead);
    }

    public static boolean getPacketStepLead() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "packet_step_lead", true);
    }

    public static void setPacketStepLead(boolean now_step_lead) {
        PreferenceUtils.putBoolean(Application.getInstance(), "packet_step_lead", now_step_lead);
    }

    public static String getUserNick() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "username", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "username", "");
        } else {
            return PreferenceUtils.getString(Application.getInstance(), "phone", "");
        }
    }

    public static void setUserIcon(String icon) {
        PreferenceUtils.putString(Application.getInstance(), "icon", icon);
    }

    public static String getUserIcon() {
        if (!TextUtils.isEmpty(PreferenceUtils.getString(Application.getInstance(), "icon", ""))) {
            return PreferenceUtils.getString(Application.getInstance(), "icon", "");
        } else {
            return "";
        }
    }


    public static void setIsOpenGuesture(boolean isOpenGuesture) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture", isOpenGuesture);
    }

    public static void setIsOpenGuestureRoot(boolean isGestrueRoot) {
        PreferenceUtils.putBoolean(Application.getInstance(), "is_open_guesture_root", isGestrueRoot);
    }

    public static boolean getIsOpenGuestureRoot() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture_root", true);
    }

    public static boolean getIsOpenGuesture() {
        return PreferenceUtils.getBoolean(Application.getInstance(), "is_open_guesture", false);
    }

    public static void saveGoLogoutTime() {
        PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
    }

    public static boolean isNeedLogout() {
        if (System.currentTimeMillis() - PreferenceUtils.getLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis()) > wait_time) {
            return true;
        } else {
            PreferenceUtils.putLong(Application.getInstance(), "go_logout_time", System.currentTimeMillis());
            return false;
        }
    }


    public static long wait_time = 1000 * 60 * 120;

    public static void saveNickName(String name) {
        PreferenceUtils.putString(Application.getInstance(), "username", name);
    }

    public static void saveUserSex(String sex) {
        PreferenceUtils.putString(Application.getInstance(), "sex", sex);
    }

    public static boolean isLogin() {
        if (PreferenceUtils.getString(Application.getInstance(), "is_login", "0").equals("1")) {
            return true;
        } else {
            return false;
        }
    }

    public static void login() {
        PreferenceUtils.putString(Application.getInstance(), "is_login", "1");
        EventBus.getDefault().post(new LoginSuccessEvent());
        saveGoLogoutTime();
    }

    public static void logout() {
        PreferenceUtils.putString(Application.getInstance(), "token", "");
        PreferenceUtils.putString(Application.getInstance(), "is_login", "");

        PreferenceUtils.putString(Application.getInstance(), "icon", "");
        PreferenceUtils.putString(Application.getInstance(), "phone", "");
        PreferenceUtils.putString(Application.getInstance(), "username", "");
        PreferenceUtils.putString(Application.getInstance(), "birthday", "");
        PreferenceUtils.putString(Application.getInstance(), "sex", "");
        PreferenceUtils.putInt(Application.getInstance(), "level", 1);
        PreferenceUtils.putString(Application.getInstance(), "money", "0");
        PreferenceUtils.putString(Application.getInstance(), "invite_code", "");
        PreferenceUtils.putString(Application.getInstance(), "invite_qrcode_url", "");
        PreferenceUtils.putString(Application.getInstance(), "commission", "0");
        PreferenceUtils.putString(Application.getInstance(), "uolevel_id", "");
        PreferenceUtils.putString(Application.getInstance(), "utlevel_id", "");
        PreferenceUtils.putString(Application.getInstance(), "isset_pay_password", "");
        PreferenceUtils.putString(Application.getInstance(), "consume_money", "0");
        EventBus.getDefault().post(new LogoutEvent());
    }

    public static boolean checkLogin() {
        if (!PreferenceUtils.getBoolean(Application.getInstance(), "is_logined", false)) {

            return false;
        }
        return true;
    }

    public static String hasInviteCode() {
        return PreferenceUtils.getString(Application.getInstance(), "invite_code", "");
    }

    public static void setInviteCode(String invite_code) {
        PreferenceUtils.putString(Application.getInstance(), "invite_code", invite_code);
    }
}
